/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.client.ZDocument;
/*     */ import com.zimbra.client.ZFolder.View;
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.common.httpclient.HttpClientUtil;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.common.util.tar.TarEntry;
/*     */ import com.zimbra.common.util.tar.TarInputStream;
/*     */ import com.zimbra.common.zmime.ZMimeMessage;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.util.JMSession;
/*     */ import java.io.ByteArrayInputStream;
/*     */ import java.io.ByteArrayOutputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.net.URI;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collections;
/*     */ import java.util.List;
/*     */ import java.util.zip.GZIPInputStream;
/*     */ import java.util.zip.ZipEntry;
/*     */ import java.util.zip.ZipInputStream;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ import javax.mail.util.SharedByteArrayInputStream;
/*     */ import javax.xml.parsers.DocumentBuilder;
/*     */ import javax.xml.parsers.DocumentBuilderFactory;
/*     */ import junit.framework.TestCase;
/*     */ import net.fortuna.ical4j.data.CalendarOutputter;
/*     */ import net.fortuna.ical4j.model.ComponentList;
/*     */ import net.fortuna.ical4j.model.Date;
/*     */ import net.fortuna.ical4j.model.ParameterList;
/*     */ import net.fortuna.ical4j.model.Property;
/*     */ import net.fortuna.ical4j.model.PropertyList;
/*     */ import net.fortuna.ical4j.model.ValidationException;
/*     */ import net.fortuna.ical4j.model.component.VEvent;
/*     */ import net.fortuna.ical4j.model.parameter.FmtType;
/*     */ import net.fortuna.ical4j.model.parameter.Value;
/*     */ import net.fortuna.ical4j.model.parameter.XParameter;
/*     */ import net.fortuna.ical4j.model.property.Attach;
/*     */ import net.fortuna.ical4j.model.property.CalScale;
/*     */ import net.fortuna.ical4j.model.property.ProdId;
/*     */ import net.fortuna.ical4j.model.property.Uid;
/*     */ import net.fortuna.ical4j.model.property.Version;
/*     */ import org.apache.commons.httpclient.Header;
/*     */ import org.apache.commons.httpclient.HttpClient;
/*     */ import org.apache.commons.httpclient.methods.GetMethod;
/*     */ import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
/*     */ import org.apache.commons.httpclient.methods.PostMethod;
/*     */ import org.junit.Test;
/*     */ import org.w3c.dom.Document;
/*     */ import org.w3c.dom.Element;
/*     */ import org.w3c.dom.NodeList;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestUserServlet
/*     */   extends TestCase
/*     */ {
/*  90 */   private static final String NAME_PREFIX = TestUserServlet.class.getSimpleName();
/*     */   private static final String USER_NAME = "user1";
/*     */   private static String id1;
/*     */   private static String id2;
/*     */   private String originalSanitizeHtml;
/*     */   
/*     */   public void setUp()
/*     */     throws Exception
/*     */   {
/*  99 */     cleanUp();
/*     */     
/*     */ 
/* 102 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 103 */     id1 = TestUtil.addMessage(mbox, NAME_PREFIX);
/* 104 */     Thread.sleep(1000L);
/* 105 */     id2 = TestUtil.addMessage(mbox, NAME_PREFIX + " 2");
/* 106 */     this.originalSanitizeHtml = TestUtil.getAccountAttr("user1", "zimbraNotebookSanitizeHtml");
/*     */   }
/*     */   
/*     */   public void testTarFormatter() throws Exception
/*     */   {
/* 111 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/*     */     
/* 113 */     verifyTarball(mbox, "//?fmt=tgz", true, true);
/* 114 */     verifyTarball(mbox, "//?fmt=tgz&body=1", true, true);
/* 115 */     verifyTarball(mbox, "//?fmt=tgz&body=0", true, false);
/* 116 */     verifyTarball(mbox, "//?fmt=tgz&meta=1", true, true);
/* 117 */     verifyTarball(mbox, "//?fmt=tgz&meta=0", false, true);
/*     */   }
/*     */   
/*     */   private void verifyTarball(ZMailbox mbox, String relativePath, boolean hasMeta, boolean hasBody) throws Exception
/*     */   {
/* 122 */     InputStream in = mbox.getRESTResource(relativePath);
/* 123 */     TarInputStream tarIn = new TarInputStream(new GZIPInputStream(in), "UTF-8");
/* 124 */     TarEntry entry = null;
/* 125 */     boolean foundMeta = false;
/* 126 */     boolean foundMessage = false;
/* 127 */     while ((entry = tarIn.getNextEntry()) != null) {
/* 128 */       if (entry.getName().endsWith(".meta")) {
/* 129 */         assertTrue("Fround " + entry.getName(), hasMeta);
/* 130 */         foundMeta = true;
/*     */       }
/* 132 */       if (entry.getName().endsWith(".eml")) {
/* 133 */         byte[] content = new byte[(int)entry.getSize()];
/* 134 */         assertEquals(content.length, tarIn.read(content));
/* 135 */         MimeMessage message = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content));
/* 136 */         byte[] body = ByteUtil.getContent(message.getInputStream(), 0);
/* 137 */         if (hasBody) {
/* 138 */           assertTrue(entry.getName() + " has no body", body.length > 0);
/*     */         } else {
/* 140 */           assertEquals(entry.getName() + " has a body", 0, body.length);
/*     */         }
/* 142 */         foundMessage = true;
/*     */       }
/*     */     }
/* 145 */     tarIn.close();
/* 146 */     assertTrue(foundMessage);
/* 147 */     if (hasMeta) {
/* 148 */       assertTrue(foundMeta);
/*     */     }
/*     */   }
/*     */   
/*     */   public void testZipFormatter() throws Exception
/*     */   {
/* 154 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/*     */     
/* 156 */     verifyZipFile(mbox, "/Inbox/?fmt=zip", true);
/* 157 */     verifyZipFile(mbox, "/Inbox/?fmt=zip&body=1", true);
/* 158 */     verifyZipFile(mbox, "/Inbox/?fmt=zip&body=0", false);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testIcsImportExport()
/*     */     throws IOException, ValidationException, ServiceException
/*     */   {
/* 167 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 168 */     String calName = NAME_PREFIX + "2ndCalendar";
/* 169 */     String calUri = String.format("/%s?fmt=ics", new Object[] { calName });
/* 170 */     TestUtil.createFolder(mbox, calName, ZFolder.View.appointment);
/* 171 */     net.fortuna.ical4j.model.Calendar calendar = new net.fortuna.ical4j.model.Calendar();
/* 172 */     calendar.getProperties().add(new ProdId("-//ZimbraTest 1.0//EN"));
/* 173 */     calendar.getProperties().add(Version.VERSION_2_0);
/* 174 */     calendar.getProperties().add(CalScale.GREGORIAN);
/* 175 */     java.util.Calendar start = java.util.Calendar.getInstance();
/* 176 */     start.set(2, 8);
/* 177 */     start.set(5, 3);
/*     */     
/* 179 */     VEvent wwII = new VEvent(new Date(start.getTime()), NAME_PREFIX + " Declarations of war");
/* 180 */     wwII.getProperties().getProperty("DTSTART").getParameters().add(Value.DATE);
/* 181 */     wwII.getProperties().add(new Uid("3-14159"));
/* 182 */     Attach attach = new Attach("Attachment.\nIsn't it short.".getBytes("us-ascii"));
/* 183 */     attach.getParameters().add(new XParameter("X-APPLE-FILENAME", "short.txt"));
/* 184 */     attach.getParameters().add(new FmtType("text/plain"));
/* 185 */     wwII.getProperties().add(attach);
/* 186 */     calendar.getComponents().add(wwII);
/* 187 */     ByteArrayOutputStream buf = new ByteArrayOutputStream();
/* 188 */     CalendarOutputter outputter = new CalendarOutputter();
/* 189 */     outputter.setValidating(false);
/* 190 */     outputter.output(calendar, buf);
/* 191 */     URI uri = mbox.getRestURI(calUri);
/* 192 */     HttpClient client = mbox.getHttpClient(uri);
/* 193 */     PostMethod post = new PostMethod(uri.toString());
/* 194 */     post.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(buf.toByteArray()), "text/calendar"));
/*     */     
/* 196 */     ZimbraLog.test.info("testIcsImportExport:ICS to be imported:%s", new Object[] { new String(buf.toByteArray()) });
/* 197 */     TestCalDav.HttpMethodExecutor.execute(client, post, 200);
/* 198 */     uri = mbox.getRestURI(calUri + "&icalAttach=inline");
/* 199 */     GetMethod get = new GetMethod(uri.toString());
/* 200 */     TestCalDav.HttpMethodExecutor executor = new TestCalDav.HttpMethodExecutor(client, get, 200);
/* 201 */     String respIcal = new String(executor.responseBodyBytes, "utf-8");
/* 202 */     ZimbraLog.test.info("testIcsImportExport:ICS exported (with icalAttach=inline):%s", new Object[] { respIcal });
/* 203 */     int attachNdx = respIcal.indexOf("ATTACH;");
/* 204 */     assertTrue("ATTACH should be present", -1 != attachNdx);
/* 205 */     String fromAttach = respIcal.substring(attachNdx);
/* 206 */     assertTrue("BINARY should be present", -1 != fromAttach.indexOf("VALUE=BINARY"));
/* 207 */     uri = mbox.getRestURI(calUri + "&icalAttach=none");
/* 208 */     get = new GetMethod(uri.toString());
/* 209 */     executor = new TestCalDav.HttpMethodExecutor(client, get, 200);
/* 210 */     respIcal = new String(executor.responseBodyBytes, "utf-8");
/* 211 */     ZimbraLog.test.info("testIcsImportExport:ICS exported (with icalAttach=none):%s", new Object[] { respIcal });
/* 212 */     assertTrue("ATTACH should be present", -1 == respIcal.indexOf("ATTACH;"));
/* 213 */     uri = mbox.getRestURI(calUri);
/* 214 */     get = new GetMethod(uri.toString());
/* 215 */     executor = new TestCalDav.HttpMethodExecutor(client, get, 200);
/* 216 */     respIcal = new String(executor.responseBodyBytes, "utf-8");
/* 217 */     ZimbraLog.test.info("testIcsImportExport:ICS exported (default - same as icalAttach=none):%s", new Object[] { respIcal });
/* 218 */     assertTrue("ATTACH should be present", -1 == respIcal.indexOf("ATTACH;"));
/*     */   }
/*     */   
/*     */   private void verifyZipFile(ZMailbox mbox, String relativePath, boolean hasBody) throws Exception
/*     */   {
/* 223 */     InputStream in = mbox.getRESTResource(relativePath);
/* 224 */     ZipInputStream zipIn = new ZipInputStream(in);
/*     */     
/* 226 */     boolean foundMessage = false;
/* 227 */     ZipEntry entry; while ((entry = zipIn.getNextEntry()) != null) {
/* 228 */       if (entry.getName().endsWith(".eml")) {
/* 229 */         ByteArrayOutputStream buf = new ByteArrayOutputStream();
/* 230 */         ByteUtil.copy(zipIn, false, buf, true);
/* 231 */         byte[] content = buf.toByteArray();
/* 232 */         MimeMessage message = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content));
/* 233 */         byte[] body = ByteUtil.getContent(message.getInputStream(), 0);
/* 234 */         if (hasBody) {
/* 235 */           assertTrue(entry.getName() + " has no body", body.length > 0);
/*     */         } else {
/* 237 */           assertEquals(entry.getName() + " has a body", 0, body.length);
/*     */         }
/* 239 */         foundMessage = true;
/*     */       }
/*     */     }
/* 242 */     zipIn.close();
/* 243 */     assertTrue(foundMessage);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testSanitizeHtmlContentType()
/*     */     throws ServiceException, IOException
/*     */   {
/* 251 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 252 */     ZDocument doc = TestUtil.createDocument(mbox, Integer.toString(16), NAME_PREFIX + " testSanitizeHtmlContentType.txt", "text/plain", "testSanitizeHtmlContentType".getBytes());
/*     */     
/*     */ 
/*     */ 
/* 256 */     Account account = TestUtil.getAccount("user1");
/* 257 */     account.setNotebookSanitizeHtml(false);
/* 258 */     checkContentType(mbox, doc);
/* 259 */     account.setNotebookSanitizeHtml(true);
/* 260 */     checkContentType(mbox, doc);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testSort() throws Exception {
/* 265 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 266 */     List<String> dateDesc = new ArrayList();
/* 267 */     dateDesc.add(id2);
/* 268 */     dateDesc.add(id1);
/* 269 */     List<String> dateAsc = new ArrayList(dateDesc);
/* 270 */     Collections.reverse(dateAsc);
/* 271 */     checkResultOrder(mbox, "/inbox?fmt=xml&query=TestUserServlet", dateDesc);
/* 272 */     checkResultOrder(mbox, "/inbox?fmt=xml&sort=dateDesc&query=TestUserServlet", dateDesc);
/* 273 */     checkResultOrder(mbox, "/inbox?fmt=xml&sort=dateAsc&query=TestUserServlet", dateAsc);
/*     */     try {
/* 275 */       checkResultOrder(mbox, "/inbox?fmt=xml&sort=rubbish&query=TestUserServlet", dateAsc);
/* 276 */       fail();
/*     */     } catch (ServiceException e) {
/* 278 */       String msg = e.getMessage();
/* 279 */       assertTrue(msg.contains("rubbish is not a valid sort order"));
/*     */     }
/*     */   }
/*     */   
/*     */   private void checkResultOrder(ZMailbox mbox, String uri, List<String> expectedOrder) throws Exception {
/* 284 */     InputStream is = mbox.getRESTResource(uri);
/* 285 */     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
/* 286 */     DocumentBuilder builder = factory.newDocumentBuilder();
/* 287 */     Document doc = builder.parse(is);
/* 288 */     NodeList nodes = doc.getElementsByTagName("m");
/* 289 */     assertEquals(expectedOrder.size(), nodes.getLength());
/* 290 */     List<String> results = new ArrayList();
/* 291 */     for (int i = 0; i < nodes.getLength(); i++) {
/* 292 */       Element node = (Element)nodes.item(i);
/* 293 */       String id = node.getAttribute("id");
/* 294 */       results.add(id);
/*     */     }
/* 296 */     assertEquals(expectedOrder, results);
/*     */   }
/*     */   
/* 299 */   private void checkContentType(ZMailbox mbox, ZDocument doc) throws ServiceException, IOException { URI uri = mbox.getRestURI("?id=" + doc.getId());
/* 300 */     HttpClient client = mbox.getHttpClient(uri);
/* 301 */     GetMethod get = new GetMethod(uri.toString());
/* 302 */     int statusCode = HttpClientUtil.executeMethod(client, get);
/* 303 */     get.releaseConnection();
/* 304 */     assertEquals(200, statusCode);
/* 305 */     assertEquals("text/plain", get.getResponseHeader("Content-Type").getValue());
/*     */   }
/*     */   
/*     */   public void tearDown()
/*     */     throws Exception
/*     */   {
/* 311 */     TestUtil.setAccountAttr("user1", "zimbraNotebookSanitizeHtml", this.originalSanitizeHtml);
/* 312 */     cleanUp();
/*     */   }
/*     */   
/*     */   private void cleanUp() throws Exception
/*     */   {
/* 317 */     TestUtil.deleteTestData("user1", NAME_PREFIX);
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception
/*     */   {
/* 322 */     TestUtil.cliSetup();
/* 323 */     TestUtil.runTest(TestUserServlet.class);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestUserServlet.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */